﻿#include "FITKAbaqusAdaptorStepDynamic.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITK_Kernel/FITKCore/FITKVec3D.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceModel/FITKAssInstanceTR.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfaceModel/FITKUnstructuredMesh.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepDynamicExplicit.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Interface/FITKInterfaceModel/FITKModelSet.h"
namespace IO
{
    QString FITKAbaqusAdaptorStepDynamic::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorStepDynamic";
    }

    bool FITKAbaqusAdaptorStepDynamic::adaptR()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepDynamicExplicit*>(_dataObj);
        if (!_stepData || !_reader) return false;
        _reader->backLine();

        //验证关键字
        QString line = _reader->readLine();
        bool isRead = false;
        if (!line.toLower().startsWith("*dynamic")) return false;
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        //绝热效应
        bool adiabatic = s.hasKey("adiabatic");
        //固定时间增量
        bool fixedTimeIncrementation = s.hasKey("fixed time incrementation");
        //用户控制
        bool directUserControl = s.hasKey("direct user control");
        //逐个元素
        bool elementByElement = s.hasKey("element by element");

        //dt
        QString dt = s["IMPROVED DT METHOD"];

        //时间缩放系数
        QString scaleFactor = s["scale factor"];



        if (fixedTimeIncrementation)
        {
            //设置时间增量类型
            _stepData->setIncrementType(Interface::FITKAbaAbstractStep::TimeIncrementType::Fixed);
            _stepData->setIncrementSizeSelection(Interface::FITKAbaAbstractStep::IncrementSizeSelection::UseElementByElementTimeIncrementEstimator);
        }
        else if (directUserControl)
        {
            //设置时间增量类型
            _stepData->setIncrementType(Interface::FITKAbaAbstractStep::TimeIncrementType::Fixed);
            _stepData->setIncrementSizeSelection(Interface::FITKAbaAbstractStep::IncrementSizeSelection::UserDefinedTimeIncrement);
            _stepData->setMaxTimeIncrement(Interface::FITKAbaAbstractStep::MaxTimeIncrement::ValueMaxTime);

        }
        else if (elementByElement)
        {
            //设置时间增量类型
            _stepData->setIncrementType(Interface::FITKAbaAbstractStep::TimeIncrementType::Automatic);
            _stepData->setStableIncrementEstimator(Interface::FITKAbaAbstractStep::StableIncrementEstimator::ElementByElement);
        }

        //初始化类型数据
        _stepData->setIncludeAdiabaticHeatingEffects(adiabatic);
        _stepData->setImproveDtMethod(!(dt.size() == 2));
        if (!scaleFactor.isEmpty())
        {
            _stepData->setTimeScalingFactor(scaleFactor.toDouble());
        }
        //显式动力学分析步解析
        if (s.hasKey("Explicit"))
        {
            isRead = readDynamicExplicit();
            if (!isRead) return false;
        }
        //隐式动力学分析步解析
        else if (s.hasKey("Standard"))
        {
            //暂不支持
            return false;
        }
        else if (s.hasKey("subspace"))
        {
            //子空间 暂时不支持
            _reader->consoleMessage(3, "Keywords are not supported yet subspace!");
            isRead = readDynamicSubspace();
            if (!isRead) return false;
        }

        while (!_reader->atEnd())
        {
            isRead = false;
            line = _reader->readLine();
            if (!line.startsWith("*")) continue;
            s.reset(line);
            QString sH = s.getHeader().toLower();
            if (sH == "bulk viscosity")
            {
                isRead = readBulkViscosity();
            }
            else if (sH == "fixed mass scaling")
            {
                isRead = readFixedMassScaling();
            }
            else if (sH == "variable mass scaling")
            {
                isRead = readVariableMassScaling();
            }
            else
            {
                if (!line.startsWith("**"))
                    break;
                else
                    isRead = true;
            }
            //解析异常 跳出条件
            if (!isRead) return false;

        }
        //读取动态分析的参数
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::adaptW()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepDynamicExplicit*>(_dataObj);
        if (!_stepData || !_writer) return false;
        writeDynamicExplicit();
        writeBulkViscosity();
        writeFixedMassScaling();
        return true;
    }



    bool FITKAbaqusAdaptorStepDynamic::readDynamicExplicit()
    {
        //由于都是一行数据 故不做回退验证行
        if (!_stepData || !_reader) return false;

        QString line = _reader->readLine();
        auto sList = line.split(",");

        //确定最大时间增量步类型
        if (_stepData->getIncrementType() == Interface::FITKAbaAbstractStep::TimeIncrementType::Automatic && sList.size() == 4)
        {
            _stepData->setMaxTimeIncrement(Interface::FITKAbaAbstractStep::MaxTimeIncrement::ValueMaxTime);
        }

        if (sList.size() == 2)
        {
            double b1 = sList[0].toDouble();
            //分析步的时间段
            double b2 = sList[1].toDouble();
            if (_stepData->getIncrementSizeSelection() == Interface::FITKAbaAbstractStep::IncrementSizeSelection::UserDefinedTimeIncrement)
                _stepData->setUserDefinedTimeIncrement(b1);
            //分析步时间
            _stepData->setTime(b2);
        }

        if (sList.size() == 4)
        {
            //暂时没用 空白值
            double n = sList[2].toDouble();
            //分析步的时间段
            double maxTimePeriod = sList[3].toDouble();
            _stepData->setMaxTimeIncrementValue(maxTimePeriod);
            _stepData->setMaxTimeIncrement(Interface::FITKAbaAbstractStep::MaxTimeIncrement::ValueMaxTime);
        }
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::readDynamicSubspace()
    {
        return false; //暂时不支持
        //由于都是一行数据 故不做回退验证行
        if (!_stepData || !_reader) return false;

        QString line = _reader->readLine();
        auto sList = line.split(",");
        if (sList.size() < 2) return false;

        //暂时没用
        double blankField = sList[0].toDouble();
        //分析步的时间段
        double timePeriod = sList[1].toDouble();
        _stepData->setMaxTimeIncrementValue(timePeriod);
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::readBulkViscosity()
    {
        if (!_stepData || !_reader) return false;

        QString line = _reader->readLine();
        auto sList = line.split(",");
        if (sList.size() != 2)
        {
            QString error = " bulk viscosity parameter exception.";
            AppFrame::FITKMessageWarning(error);
            return false;
        }
        double p1 = sList[0].toDouble();
        double p2 = sList[1].toDouble();
        _stepData->setLinearBulkViscosityParameter(p1);
        _stepData->setQudraticBulkViscosityParameter(p2);
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::readFixedMassScaling()
    {
        if (!_stepData || !_reader) return false;
        //回退一行，读取关键字
        _reader->backLine();
        QString line = _reader->readLine();
        //校验行
        if (!line.toLower().startsWith("*fixed mass scaling")) return false;
        InpKeyLineSpliter s(line, _reader);
        //创建质量缩放
        Interface::FITKAbaMassScalingDefinition* massScaling = _stepData->createMassScalingDefinition();
        if (!massScaling) return false;
        //Reinitialize mass
        if (s.count() == 0)
        {
            massScaling->setMassScalingObjective(Interface::FITkMassScalarEnum::MassScalingObjective::ReinitializeMass);
            massScaling->setMassScalingApplicationRegion(Interface::FITkMassScalarEnum::MassScalingApplicationRegion::WholeModel);
            //设置显示分析步质量缩放类型
            _stepData->setMassScaling(Interface::FITKAbaAbstractStep::MassScaling::Definitions);
            return true;
        }
        else if (s.count() == 1 && s.hasKey("elset"))
        {
            massScaling->setMassScalingObjective(Interface::FITkMassScalarEnum::MassScalingObjective::ReinitializeMass);
            massScaling->setMassScalingApplicationRegion(Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set);
            QString elset = s["elset"];
            int modelID = 0;
            int setID = tool.getSetIDByName(elset, -1, modelID);
            if (setID == -1) return false;
            Interface::FITKBoundaryLocation* locationData = massScaling->getLocation();
            if (locationData)
            {
                locationData->setModel(modelID);
                locationData->setComponent(setID);
            }
            //设置显示分析步质量缩放类型
            _stepData->setMassScaling(Interface::FITKAbaAbstractStep::MassScaling::Definitions);
            return true;
        }
        //Semi-automatic mass scaling
        massScaling->setMassScalingObjective(Interface::FITkMassScalarEnum::MassScalingObjective::SemiAutomaticMassScaling);
        //init 数据
        {
            massScaling->setMassScalingApplicationRegion(Interface::FITkMassScalarEnum::MassScalingApplicationRegion::WholeModel);
            massScaling->setMassScalingApplicationScale(Interface::FITkMassScalarEnum::MassScalingApplicationScale::AtBeginningOfStep);
            massScaling->setScaleByFactor(false);
            massScaling->setScalerToTargetTimeIncrement(false);
        }
        bool ok = false;
        //设置Region类型为set
        if (s.hasKey("elset"))
        {
            QString elset = s["elset"];
            int modelID = 0;
            massScaling->setMassScalingApplicationRegion(Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set);
            int setID = tool.getSetIDByName(elset, -1, modelID);
            if (setID == -1) return false;
            Interface::FITKBoundaryLocation* locationData = massScaling->getLocation();
            if (locationData)
            {
                locationData->setModel(modelID);
                locationData->setComponent(setID);
            }
        }
        //设置类型-ScaleByFactor-true
        if (s.hasKey("factor"))
        {
            QString factor = s["factor"];
            double faValue = factor.toDouble(&ok);
            if (!ok)return false;
            massScaling->setScaleByFactor(true);
            massScaling->setScalerFactor(faValue);
        }
        //设置类型-Scaler To Target Time Increment-true
        if (s.hasKey("dt"))
        {
            QString dt = s["dt"];
            QString t = s["type"].trimmed().toLower();
            double dtValue = dt.toDouble(&ok);
            if (!ok)return false;
            massScaling->setScalerToTargetTimeIncrement(true);
            massScaling->setTargetIncrement(dtValue);
            //type 缩放单元质量
            if (t == "uniform")
            {
                massScaling->setScaleElementMass(Interface::FITkMassScalarEnum::MassScalingScaleElementMass::UniformlyToSatisfyTarget);
            }
            else if (t == "below min") //默认
            {
                massScaling->setScaleElementMass(Interface::FITkMassScalarEnum::MassScalingScaleElementMass::IfBelowMinimumTarget);
            }
            else if (t == "set equal dt")
            {
                massScaling->setScaleElementMass(Interface::FITkMassScalarEnum::MassScalingScaleElementMass::NonuniformlyToEqualTarget);
            }
        }
        //设置显示分析步质量缩放类型
        _stepData->setMassScaling(Interface::FITKAbaAbstractStep::MassScaling::Definitions);
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::readVariableMassScaling()
    {
        if (!_stepData || !_reader) return false;
        //回退一行，读取关键字
        _reader->backLine();
        QString line = _reader->readLine();
        //校验行
        if (!line.toLower().startsWith("*variable mass scaling")) return false;
        InpKeyLineSpliter s(line, _reader);
        bool ok = false;
        //创建质量缩放
        Interface::FITKAbaMassScalingDefinition* massScaling = _stepData->createMassScalingDefinition();
        if (!massScaling) return false;
        //disable mass scaling throuhout step
        if (s.count() == 0)
        {
            massScaling->setMassScalingObjective(Interface::FITkMassScalarEnum::MassScalingObjective::DisaleMassScalingThroughoutStep);
            //设置显示分析步质量缩放类型
            _stepData->setMassScaling(Interface::FITKAbaAbstractStep::MassScaling::Definitions);
            return true;
        }
        //Semi-automatic mass scaling
        if (s.hasKey("dt"))
        {
            massScaling->setMassScalingObjective(Interface::FITkMassScalarEnum::MassScalingObjective::SemiAutomaticMassScaling);
            massScaling->setMassScalingApplicationRegion(Interface::FITkMassScalarEnum::MassScalingApplicationRegion::WholeModel);
            massScaling->setMassScalingApplicationScale(Interface::FITkMassScalarEnum::MassScalingApplicationScale::ThroughoutStep);
            massScaling->setScaleByFactor(false);
            massScaling->setScalerToTargetTimeIncrement(true);
            if (s.hasKey("elset"))
            {
                QString elset = s["elset"];
                int modelID = 0;
                massScaling->setMassScalingApplicationRegion(Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set);
                int setID = tool.getSetIDByName(elset, -1, modelID);
                if (setID == -1) return false;
                Interface::FITKBoundaryLocation* locationData = massScaling->getLocation();
                if (locationData)
                {
                    locationData->setModel(modelID);
                    locationData->setComponent(setID);
                }
            }
            QString dt = s["dt"];
            QString t = s["type"].trimmed().toLower();
            double dtValue = dt.toDouble(&ok);
            if (!ok)return false;
            massScaling->setTargetIncrement(dtValue);
            //type 缩放单元质量
            if (t == "uniform")
                massScaling->setScaleElementMass(Interface::FITkMassScalarEnum::MassScalingScaleElementMass::UniformlyToSatisfyTarget);
            else if (t == "below min") //默认
                massScaling->setScaleElementMass(Interface::FITkMassScalarEnum::MassScalingScaleElementMass::IfBelowMinimumTarget);
            else if (t == "set equal dt")
                massScaling->setScaleElementMass(Interface::FITkMassScalarEnum::MassScalingScaleElementMass::NonuniformlyToEqualTarget);
            //Frequency
            if (s.hasKey("number interval"))
            {
                int atEqu = s["number interval"].trimmed().toInt(&ok);
                if (!ok)return false;
                massScaling->setMassScalingFrequency(Interface::FITkMassScalarEnum::MassScalingFrequency::AtEqualIntervals);
                massScaling->setNumberOfInvtervals(atEqu);
            }
            else if (s.hasKey("frequency"))
            {
                int everty = s["frequency"].trimmed().toInt(&ok);
                if (!ok)return false;
                massScaling->setMassScalingFrequency(Interface::FITkMassScalarEnum::MassScalingFrequency::EvertyIncrements);
                massScaling->setEveryIncrementNumber(everty);
            }
            //设置显示分析步质量缩放类型
            _stepData->setMassScaling(Interface::FITKAbaAbstractStep::MassScaling::Definitions);
            return true;
        }
        if (!this->readAutomaticMassScaling(massScaling, line))return false;
        _stepData->setMassScaling(Interface::FITKAbaAbstractStep::MassScaling::Definitions);
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::readAutomaticMassScaling(Interface::FITKAbaMassScalingDefinition* massScaling, QString line)
    {
        if (!_stepData || !_reader) return false;
        bool ok = false;
        //关键字数据
        QHash<QString, QString> keyValueHash{};
        while (!_reader->atEnd())
        {
            //获取关键字数据
            QStringList strList = line.split(',');
            for (int i = 0; i < strList.size(); ++i)
            {
                QString str = strList.at(i);
                //不包含等于号的处理
                if (!str.contains("="))
                {
                    keyValueHash.insert(str.simplified(), QString());
                    continue;
                    QStringList key_value = strList.at(i).split('=');
                    if (key_value.size() != 2) continue;
                    //包含等于号的需要解析value
                    const QString k = key_value.at(0).simplified();
                    const QString v = key_value.at(1).simplified();
                    keyValueHash.insert(k, v);
                }
            }
            line = _reader->readLine();
            if (line.startsWith("*"))break;
        }
        //Automatic mass scaling
        massScaling->setMassScalingObjective(Interface::FITkMassScalarEnum::MassScalingObjective::AutomaticMassScaling);
        if (!keyValueHash.contains("type") || keyValueHash.value("type").trimmed().toLower() != "rolling") return false;
        massScaling->setMassScalingApplicationRegion(Interface::FITkMassScalarEnum::MassScalingApplicationRegion::WholeModel);
        massScaling->setMassScalingApplicationScale(Interface::FITkMassScalarEnum::MassScalingApplicationScale::ThroughoutStep);
        if (keyValueHash.contains("elset"))
        {
            QString elset = keyValueHash["elset"];
            int modelID = 0;
            massScaling->setMassScalingApplicationRegion(Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set);
            int setID = tool.getSetIDByName(elset, -1, modelID);
            if (setID == -1) return false;
            Interface::FITKBoundaryLocation* locationData = massScaling->getLocation();
            if (locationData)
            {
                locationData->setModel(modelID);
                locationData->setComponent(setID);
            }
        }
        if (!keyValueHash.contains("feed rate") || !keyValueHash.contains("extruded length") || !keyValueHash.contains("cross section"))return false;
        //Type-Rolling
        double rate = keyValueHash["feed rate"].trimmed().toDouble(&ok);
        if (!ok)return false;
        double length = keyValueHash["extruded length"].trimmed().toDouble(&ok);
        if (!ok)return false;
        double cross = keyValueHash["cross section"].trimmed().toDouble(&ok);
        if (!ok)return false;

        //Frequency
        if (!keyValueHash.contains("number interval"))
        {
            int atEqu = keyValueHash["number interval"].trimmed().toInt(&ok);
            if (!ok)return false;
            massScaling->setMassScalingFrequency(Interface::FITkMassScalarEnum::MassScalingFrequency::AtEqualIntervals);
            massScaling->setNumberOfInvtervals(atEqu);
        }
        else if (!keyValueHash.contains("frequency"))
        {
            int everty = keyValueHash["frequency"].trimmed().toInt(&ok);
            if (!ok)return false;
            massScaling->setMassScalingFrequency(Interface::FITkMassScalarEnum::MassScalingFrequency::EvertyIncrements);
            massScaling->setEveryIncrementNumber(everty);
        }
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::writeDynamicExplicit()
    {
        // 写出节点函数主体
        QTextStream* stream = _writer->getStream();
        QString outputTitle;
        QString output;
        outputTitle += "*Dynamic, Explicit";
        if (_stepData->getStableIncrementEstimator() == Interface::FITKAbaAbstractStep::StableIncrementEstimator::ElementByElement)
            outputTitle += ", element by element";
        else if (_stepData->getIncrementSizeSelection() == Interface::FITKAbaAbstractStep::IncrementSizeSelection::UserDefinedTimeIncrement &&
            _stepData->getMaxTimeIncrement() == Interface::FITKAbaAbstractStep::MaxTimeIncrement::ValueMaxTime)
            outputTitle += ", direct user control";
        else if (_stepData->getIncrementSizeSelection() == Interface::FITKAbaAbstractStep::IncrementSizeSelection::UseElementByElementTimeIncrementEstimator &&
            _stepData->getIncrementType() == Interface::FITKAbaAbstractStep::TimeIncrementType::Fixed)
            outputTitle += ", fixed time incrementation";



        if (_stepData->getTimeScalingFactor() != 1)
        {
            outputTitle += QString(", scale factor=%1").arg(_stepData->getTimeScalingFactor());
        }
        *stream << outputTitle << endl;
        double d1 = _stepData->getUserDefinedTimeIncrement();
        double stepTime = _stepData->getTime();
        double d3 = 0;
        double maxTime = _stepData->getMaxTimeIncrementValue();
        _stepData->getMaxTimeIncrement();

        //自动
        if (_stepData->getIncrementType() == Interface::FITKAbaAbstractStep::TimeIncrementType::Automatic)
        {
            //数值
            if (_stepData->getMaxTimeIncrement() == Interface::FITKAbaAbstractStep::MaxTimeIncrement::ValueMaxTime)
            {
                if (_stepData->getIncrementSizeSelection() == Interface::FITKAbaAbstractStep::IncrementSizeSelection::UserDefinedTimeIncrement)
                    output += _writer->double2String(d1, 9, 0);
                output += ", " + _writer->double2String(stepTime, 9, 0);
                output += ", ";
                output += ", " + _writer->double2String(maxTime, 9, 0);
            }
            else
            {
                if (_stepData->getIncrementSizeSelection() == Interface::FITKAbaAbstractStep::IncrementSizeSelection::UserDefinedTimeIncrement)
                    output += _writer->double2String(d1, 9, 0);
                output += ", " + _writer->double2String(stepTime, 9, 0);
            }
        }
        else //固定
        {
            if (_stepData->getIncrementSizeSelection() == Interface::FITKAbaAbstractStep::IncrementSizeSelection::UserDefinedTimeIncrement)
                output += _writer->double2String(d1, 9, 0);
            output += ", " + _writer->double2String(stepTime, 9, 0);
        }

        *stream << output << endl;
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::writeBulkViscosity()
    {
        // 写出节点函数主体
        QTextStream* stream = _writer->getStream();
        double bv1 = _stepData->getLinearBulkViscosityParameter();
        double bv2 = _stepData->getQudraticBulkViscosityParameter();
        *stream << "*Bulk Viscosity" << endl;
        *stream << _writer->double2String(bv1) << ", " << _writer->double2String(bv2);
        *stream << endl;
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::writeFixedMassScaling()
    {
        if (!_stepData || !_writer)return false;
        if (_stepData->getMassScaling() == Interface::FITKAbaAbstractStep::MassScaling::FromPreviousStep)return true;
        // 写出节点函数主体
        int count = _stepData->getNumberOfMassScalingDefinition();
        bool writeOK = false;
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaMassScalingDefinition* massScaling = _stepData->getMassScalingDefinition(i);
            Interface::FITkMassScalarEnum::MassScalingObjective type = massScaling->getMassScalingObjectvie();
            if (type == Interface::FITkMassScalarEnum::MassScalingObjective::SemiAutomaticMassScaling)
                writeOK = this->writeSemiAutomaticMassScaling(massScaling);
            else if (type == Interface::FITkMassScalarEnum::MassScalingObjective::AutomaticMassScaling)
                writeOK = this->writeAutomaticMassScaling(massScaling);
            else if (type == Interface::FITkMassScalarEnum::MassScalingObjective::ReinitializeMass)
                writeOK = this->writeReinitializeMass(massScaling);
            else if (type == Interface::FITkMassScalarEnum::MassScalingObjective::DisaleMassScalingThroughoutStep)
                writeOK = this->writeDisaleMassScalingThroughoutStep(massScaling);
            if (!writeOK)break;
        }

        
        //if(_stepData->get)
        return writeOK;
    }

    bool FITKAbaqusAdaptorStepDynamic::writeSemiAutomaticMassScaling(Interface::FITKAbaMassScalingDefinition* massScaling)
    {
        QTextStream* stream = _writer->getStream();
        //写出注释
        *stream << "** Mass Scaling: Semi-Automatic" << endl;
        QString setName;
        if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::WholeModel)
            *stream << "**               Whole Model" << endl;
        else if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set)
        {
            Interface::FITKBoundaryLocation* locationData = massScaling->getLocation();
            if (locationData)
            {
                if (!locationData->getComponentObj())return false;
                setName = locationData->getComponentObj()->getDataObjectName();
                if (locationData->getModelObj() && locationData->getModelObj()->getAbsModelType() != Interface::FITKModelEnum::AbsModelType::Assembly)
                    setName = QString("%1.%2").arg(locationData->getModelObj()->getDataObjectName()).arg(setName);
            }
            *stream << QString("**               %1").arg(setName) << endl;
        }
        QString lineOne;
        //scale类型
        if (massScaling->getMassScalingApplicationScale() == Interface::FITkMassScalarEnum::MassScalingApplicationScale::AtBeginningOfStep)
            lineOne = QString("*Fixed Mass Scaling");
        else if (massScaling->getMassScalingApplicationScale() == Interface::FITkMassScalarEnum::MassScalingApplicationScale::ThroughoutStep)
            lineOne = QString("*Variable Mass Scaling");
        //Region类型
        if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set)
            lineOne += QString(", elset=%1").arg(setName);
        if (massScaling->getScalerToTargetTimeIncrement() || massScaling->getMassScalingApplicationScale() == Interface::FITkMassScalarEnum::MassScalingApplicationScale::ThroughoutStep)
        {
            double dt = massScaling->getTargetIncrement();
            Interface::FITkMassScalarEnum::MassScalingScaleElementMass type = massScaling->getScaleElementMass();
            if (type == Interface::FITkMassScalarEnum::MassScalingScaleElementMass::UniformlyToSatisfyTarget)
                lineOne += QString(", dt=%1, type=%2").arg(_writer->double2String(dt, 9, 0)).arg("uniform");
            else if (type == Interface::FITkMassScalarEnum::MassScalingScaleElementMass::IfBelowMinimumTarget)
                lineOne += QString(", dt=%1, type=%2").arg(_writer->double2String(dt, 9, 0)).arg("below min");
            else if (type == Interface::FITkMassScalarEnum::MassScalingScaleElementMass::NonuniformlyToEqualTarget)
                lineOne += QString(", dt=%1, type=%2").arg(_writer->double2String(dt, 9, 0)).arg("set equal dt");
        }
        if (massScaling->getScaleByFactor() && massScaling->getMassScalingApplicationScale() == Interface::FITkMassScalarEnum::MassScalingApplicationScale::AtBeginningOfStep)
        {
            double faValue = massScaling->getScalerFactor();
            lineOne += QString(", factor=%1").arg(_writer->double2String(faValue, 9, 0));
        }
        if (massScaling->getMassScalingApplicationScale() == Interface::FITkMassScalarEnum::MassScalingApplicationScale::ThroughoutStep)
        {
            Interface::FITkMassScalarEnum::MassScalingFrequency frequencyType = massScaling->getMassScalingFrequency();
            if (frequencyType == Interface::FITkMassScalarEnum::MassScalingFrequency::EvertyIncrements)
                lineOne += QString(", frequency=%1").arg(_writer->int2String(massScaling->getEveryIncrementNumber(), 0));
            else if (frequencyType == Interface::FITkMassScalarEnum::MassScalingFrequency::AtEqualIntervals)
                lineOne += QString(", number interval=%1").arg(_writer->int2String(massScaling->getNumberOfIntervals(), 0));
        }
        *stream << lineOne << endl;
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::writeAutomaticMassScaling(Interface::FITKAbaMassScalingDefinition* massScaling)
    {
        QTextStream* stream = _writer->getStream();
        //写出注释
        *stream << "** Mass Scaling: Automatic" << endl;
        QString setName;
        if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::WholeModel)
            *stream << "**               Whole Model" << endl;
        else if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set)
        {
            Interface::FITKBoundaryLocation* locationData = massScaling->getLocation();
            if (locationData)
            {
                if (!locationData->getComponentObj())return false;
                setName = locationData->getComponentObj()->getDataObjectName();
                if (locationData->getModelObj() && locationData->getModelObj()->getAbsModelType() != Interface::FITKModelEnum::AbsModelType::Assembly)
                    setName = QString("%1.%2").arg(locationData->getModelObj()->getDataObjectName()).arg(setName);
            }
            *stream << QString("**               %1").arg(setName) << endl;
        }
        QString lineOne = QString("*Variable Mass Scaling");
        if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set)
            lineOne += QString(", elset=%1").arg(setName);
        lineOne += QString(", type=rolling");
        //Type-Rolling
        double rate = 0.0;
        double length = 0.0;
        double cross = 0.0;
        lineOne += QString(", feed rate=%1,").arg(_writer->double2String(rate, 9, 0));
        *stream << lineOne << endl;
        lineOne = QString("extruded length=%1, cross section=%2").arg(_writer->double2String(length, 9, 0)).arg(_writer->double2String(cross, 9, 0));
        Interface::FITkMassScalarEnum::MassScalingFrequency frequencyType = massScaling->getMassScalingFrequency();
        if (frequencyType == Interface::FITkMassScalarEnum::MassScalingFrequency::EvertyIncrements)
            lineOne += QString(", frequency=%1").arg(_writer->int2String(massScaling->getEveryIncrementNumber(), 0));
        else if (frequencyType == Interface::FITkMassScalarEnum::MassScalingFrequency::AtEqualIntervals)
            lineOne += QString(", number interval=%1").arg(_writer->int2String(massScaling->getNumberOfIntervals(), 0));
        *stream << lineOne << endl;
        return true;
    }
    
    bool FITKAbaqusAdaptorStepDynamic::writeReinitializeMass(Interface::FITKAbaMassScalingDefinition* massScaling)
    {
        QTextStream* stream = _writer->getStream();
        //写出注释
        *stream << "** Mass Scaling: Reinitialize Mass" << endl;
        QString setName;
        if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::WholeModel)
            *stream << "**               Whole Model" << endl;
        else if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set)
        {
            Interface::FITKBoundaryLocation* locationData = massScaling->getLocation();
            if (locationData)
            {
                if (!locationData->getComponentObj())return false;
                setName = locationData->getComponentObj()->getDataObjectName();
                if (locationData->getModelObj() && locationData->getModelObj()->getAbsModelType() != Interface::FITKModelEnum::AbsModelType::Assembly)
                    setName = QString("%1.%2").arg(locationData->getModelObj()->getDataObjectName()).arg(setName);
            }
            *stream << QString("**               %1").arg(setName) << endl;
        }
        QString lineOne = QString("*Fixed Mass Scaling");
        if (massScaling->getMassScalingApplicationRegion() == Interface::FITkMassScalarEnum::MassScalingApplicationRegion::Set)
            lineOne += QString(", elset=%1").arg(setName);
        *stream << lineOne << endl;
        return true;
    }

    bool FITKAbaqusAdaptorStepDynamic::writeDisaleMassScalingThroughoutStep(Interface::FITKAbaMassScalingDefinition* massScaling)
    {
        QTextStream* stream = _writer->getStream();
        //写出注释
        *stream << "** Mass Scaling: Disable Throughout Step" << endl;
        QString lineOne = QString("*Variable Mass Scaling");
        *stream << lineOne << endl;
        return true;
    }

}
